package net.orange.framework.model;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import net.orange.architecture.model.BaseValueObject;
import net.orange.architecture.validator.NotDulicateValidator;
import net.orange.architecture.validator.ObjectNotNullValidator;
import net.orange.architecture.validator.StringNotNullValidator;
import net.orange.architecture.validator.Validator;
import net.orange.framework.validator.GroupCodeFormatValidator;
import net.orange.framework.validator.GroupHasMenuValidator;
import net.orange.framework.validator.GroupHasResourceValidator;
import net.orange.framework.validator.GroupHasUserValidator;

/**
 * The group department, which it define the system user function group. And it
 * use for function authority control usually.
 */
@Entity
@Table(name = "t_group")
public class Group extends BaseValueObject {

	private static final long serialVersionUID = 5756112715826449821L;

	/**
	 * The group code
	 */
	@Column(name = "group_code")
	private String group_code;

	/**
	 * The group name
	 */
	@Column(name = "group_name")
	private String group_name;

	/**
	 * The description
	 */
	@Column(name = "description")
	private String description;

	/**
	 * The department.
	 */
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	@JoinColumn(name = "dept_pk", insertable = true, updatable = true)
	private Department department;

	/**
	 * @return Returns the description.
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @return Returns the group_code.
	 */
	public String getGroup_code() {
		return group_code;
	}

	/**
	 * @return Returns the group_name.
	 */
	public String getGroup_name() {
		return group_name;
	}

	/**
	 * @param description
	 *            The description to set.
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param group_code
	 *            The group_code to set.
	 */
	public void setGroup_code(String group_code) {
		this.group_code = group_code;
	}

	/**
	 * @param group_name
	 *            The group_name to set.
	 */
	public void setGroup_name(String group_name) {
		this.group_name = group_name;
	}

	/**
	 * @return Returns the department.
	 */
	public Department getDepartment() {
		return department;
	}

	/**
	 * @param department
	 *            The department to set.
	 */
	public void setDepartment(Department department) {
		this.department = department;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.model.BaseValueObject#satisfy(java.util.Collection
	 * )
	 */
	public boolean satisfy(Collection<String> msg) throws Exception {
		if (msg == null)
			msg = new ArrayList<String>();

		Validator validator = new Validator(msg);

		validator
				.add(new StringNotNullValidator(getGroup_code(), "group. code"));
		validator
				.add(new StringNotNullValidator(getGroup_name(), "group. name"));
		validator
				.add(new ObjectNotNullValidator(getDepartment(), "department"));
		validator.add(new GroupCodeFormatValidator(this));

		validator.add(new NotDulicateValidator(this, "group_code", "group"));

		validator.validate();

		return (msg == null || msg.size() <= 0);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * net.orange.architecture.model.BaseValueObject#canRemove(java.util.Collection
	 * )
	 */
	public boolean canRemove(Collection<String> msg) throws Exception {
		if (msg == null)
			msg = new ArrayList<String>();

		Validator validator = new Validator(msg);
		validator.add(new GroupHasMenuValidator(this));
		validator.add(new GroupHasUserValidator(this));
		validator.add(new GroupHasResourceValidator(this));
		validator.validate();

		return (msg == null || msg.size() <= 0);
	}
}